var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: {
      searchValue: '',
      showClearBtn: false
    },
    searchResult: [],
    zhuanjia: [{
      src: "/yiliao/images/jiang.png",
    }],
    hidden: true,
    rotateIndex: '',
    animationData: {},
    statusImage: 'https://lg-o8nytxik-1257013711.cos.ap-shanghai.myqcloud.com/jiazai.png',
    statusClass: 'load'

  },
  fenlei: function() {
    this.setData({
      hidden: !this.data.hidden
    })
  },
  xzdoc: function(e) {
    console.log(e)
    //点击的时候同时触发关注医生，这样医生就可以看到患者信息
    var z_name = e.currentTarget.dataset.z_name
    var zid = e.currentTarget.dataset.zid
    app.util.request({
      'url': 'entry/wxapp/SaveCollect1',
      data: {
        openid: wx.getStorageSync('openid'),
        goods_id: zid,
        cerated_type: 0
      },
      dataType: 'json',
      success: function(res) {
        console.log(res);
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        prevPage.setData({
          z_name: z_name
        })
        wx.navigateBack({
          delta: 1
        })

      }
    })
  },
  //下拉刷新
  onPullDownRefresh: function() {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    wx.showNavigationBarLoading() //在标题栏中显示加载
    var that = this
    app.util.request({
      'url': 'entry/wxapp/Zhuanjia',
      success: function(res) {
        var info = res.data.data
        that.setData({
          zhuanjia: info
        })
      },
      fail: function(err) {
        console.log(err)
      },
    })
    //模拟加载
    setTimeout(function() {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this;
    var now = new Date().getTime();
    if (options.id) {
      app.util.request({
        'url': 'entry/wxapp/Kszhuanjia',
        data: {
          id: options.id
        },
        success: function(res) {
          console.log(res)
          var info = res.data.data
          that.setData({
            zhuanjia: info,
            time: new Date().getTime() - now
          })
          that.anima();
        },
        fail: function(err) {
          console.log(err)
        },
      })
    } else {
      app.util.request({
        'url': 'entry/wxapp/Zhuanjia',
        success: function(res) {
          //console.log(res)
          var info = res.data.data
          that.setData({
            zhuanjia: info,
            time: new Date().getTime() - now
          });
          that.anima();
        },
        fail: function(err) {
          console.log(err)
        },
      })
    }
    app.util.request({
      'url': 'entry/wxapp/Base',
      success: function(res) {
        //console.log(res)
        that.setData({
          bq_thumb: res.data.data.bq_thumb,
          bq_name: res.data.data.bq_name
        })
      },
      fail: function(err) {
        console.log(err)
      },

    })

  },
  bindFocus: function() {
    wx.navigateTo({
      url: './serch/serch'
    })
  },
  //电话咨询
  showClick: function(e) {
    var that = this
    var id = e.currentTarget.dataset.zid
    app.util.request({
      'url': "entry/wxapp/Zhuanjiaxiangqing",
      data: {
        id: id
      },
      success: function(res) {
        console.log(res)
        that.setData({
          xiangqing: res.data.data,
        })
      },
      fail: function(err) {
        // console.log(err)
      },
    })
    console.log(e)
    this.setData({
      overflow: true
    })
  },
  hideClick: function() {
    this.setData({
      overflow: false
    })
  },
  //支付
  payClick: function(e) {
    console.log(e)
    var webviewurl = e.currentTarget.dataset.url
    var name = e.currentTarget.dataset.z_name
    var openid = wx.getStorageSync('openid')
    var tw_money = e.currentTarget.dataset.money
    app.util.request({
      'url': 'entry/wxapp/Pay',
      header: {
        "Content-Type": "application/xml"
      },
      method: "GET",
      data: {
        openid: openid,
        z_tw_money: tw_money
      },
      success: function(res) {
        console.log(res);
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr, //字符串随机数
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function(res) {
            app.util.request({
              'url': "entry/wxapp/Joninmoney",
              data: {
                use_openid: openid,
                leixing: '电话',
                name: name,
                pay: tw_money
              },
              header: {
                'content-type': 'application/json'
              },
              success: function(res) {
                console.log(res)

              },
              fail: function(err) {
                console.log(err)
              },
            })
            wx.navigateTo({
              url: '/hyb_yl/zhuanjiasubpages/pages/webview/webview?src=' + webviewurl,
            })
            // app.util.request({
            //   'url': "entry/wxapp/Doctorqs",
            //   data: {
            //     question: textarea,
            //     user_picture: arr,
            //     user_payment: tw_money,
            //     user_openid: openid,
            //     savant_openid: doctor_openid,
            //     sj_type: sj_type,
            //     p_id: p_id,
            //     q_username: q_username,
            //     q_thumb: q_thumb,
            //     q_docthumb: q_docthumb,
            //     q_zhiwei: q_zhiwei,
            //     q_dname: q_dname,
            //     h_pic: 1
            //   },
            //   header: { 'content-type': 'application/json' },
            //   success: function (res) {
            //     // console.log(res)
            //     wx.showLoading({
            //       title: "提问成功",
            //     })
            //     setTimeout(function () {
            //       wx.hideLoading();
            //       wx.redirectTo({
            //         url: '../wodetiwen/wodetiwen',
            //       })
            //     }, 800)
            //   },
            //   fail: function (err) {
            //     console.log(err)
            //   },
            // })
          }
        })
      },
      fail: function(err) {
        console.log(err);
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {


  },
  anima: function() {
    // 创建动画
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease'
    })
    this.animation = animation
    // 图片旋转
    this.imageRotators()
  },
  imageRotators: function() {
    //连续动画需要添加定时器,所传参数每次+1就行
    this.timeInterval = setInterval(function() {
      this.data.rotateIndex = this.data.rotateIndex + 1;
      this.animation.rotateZ(360 * (this.data.rotateIndex)).step()
      this.setData({
        animationData: this.animation.export()
      })
    }.bind(this), 100)
    // 请求API接口或者别的操作
    this.request()
  },
  // 停止旋转
  stopRotators: function() {
    // console.log(this.timeInterval)
    if (this.timeInterval > 0) {
      clearInterval(this.timeInterval)
      this.timeInterval = 0
    }
  },
  // 请求API接口或者别的操作
  request: function(e) {
    var that = this
    console.log(that.data.time)
    setTimeout(function() {
      // 停止旋转
      that.stopRotators()
      console.log('请求到了数据或者操作完成,停止旋转')
      // 这里是根据自己的业务逻辑设置
      var data = true
      if (data) {
        that.setData({
          statusImage: 'https://lg-o8nytxik-1257013711.cos.ap-shanghai.myqcloud.com/erddd.png',
          statusClass: 'success'
        })
      } else {
        that.setData({
          statusImage: 'https://lg-o8nytxik-1257013711.cos.ap-shanghai.myqcloud.com/ask.png',
          statusClass: 'failing'
        })
      }
    }, 100)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //输入内容时
  searchActiveChangeinput: function(e) {
    const val = e.detail.value;
    this.setData({
      'search.showClearBtn': val != '' ? true : false,
      'search.searchValue': val
    })
  },
  //点击清除搜索内容
  searchActiveChangeclear: function(e) {
    this.setData({
      'search.showClearBtn': false,
      'search.searchValue': ''
    })
  },
  //点击聚集时
  // focusSearch: function () {
  //   if (this.data.search.searchValue) {
  //     this.setData({
  //       'search.showClearBtn': true
  //     })
  //   }
  // },
  // 
  //搜索提交
  searchSubmit: function() {
    const val = this.data.search.searchValue;
    if (val) {
      const that = this,
        app = getApp();
      wx.showLoading({
        title: '搜索中'
      })
      setTimeout(function() {
        wx.hideLoading()
      }, 2000);
      app.util.request({
        url: "entry/wxapp/Activity",
        data: {
          keywords: val,
        },
        method: 'GET',
        success: function(res) {
          console.log(res.data.length)
          if (!res.data.length) {
            wx.showToast({
              title: '暂无此医生',
            })
          }
          let searchResult = res.data;
          const len = searchResult.length;
          for (let i = 0; i < len; i++) {
            searchResult[i]['team_avator'] = app.globalData.STATIC_SOURCE + searchResult[i]['team_avator'];
          }
          that.setData({
            searchResult: searchResult,
            'search.showClearBtn': false,
          })
        },
        fail: function() {},
        complete: function() {
          wx.hideToast();
        }
      })
    }
  },

})